﻿@page "/CachereturnPage"
@inject PickingServer pickingServer
@using System.ComponentModel
@using AntDesign.TableModels
@using ShanjinPDAUI.ModelEntity
@inject ModalService _modalService
@inject MessageService _message
<h3>退/下料</h3>
<div>
    <Row>
        <Col Span="24">请选择缓存架</Col>
    </Row>
    <Row>
        <Col Span="24">
        <Select style="width:100%" DataSource="@FullCaches"
                AllowClear="true"
                ValueOnClear="@(0)"
                @bind-Value="@fullselet"
                ValueName="@nameof(FullCache_Entity.Deviceid)"
                LabelName="@nameof(FullCache_Entity.Remark)">
        </Select>
        </Col>
    </Row>
    <br>
    <Row>
        <Col Span="24">
        <Button style="width:100%" Type="@ButtonType.Primary" @onclick="@Creat">缓存架退料</Button>
        </Col>
    </Row>
    <br>

</div>

@code {
    ObservableCollection<FullCache_Entity> FullCaches { get; set; } = new ObservableCollection<FullCache_Entity>();
    ObservableCollection<Machine_Entity> machines { get; set; } = new ObservableCollection<Machine_Entity>();
    int fullselet { get; set; } = 0;
    bool Flag { get; set; } = false;
    protected override async Task OnInitializedAsync()
    {
        var fullCache_s = await pickingServer.GetReturnGetChaches();
        var machiness = await pickingServer.GetMachine();
        if (fullCache_s != null)
        {
            FullCaches.Clear();
            foreach (var item in fullCache_s)
            {
                FullCaches.Add(item);
            }
        }
        if (machiness != null)
        {
            machines.Clear();
            foreach (var item in machiness)
            {
                machines.Add(item);
            }
        }


        base.OnInitialized();
    }
    private async Task Creat()
    {
        _modalService.Confirm(new ConfirmOptions()
            {
                Title = "提示",
                Icon =@<Icon Type="exclamation-circle" Theme="Outline"></Icon>,
            Content = "是否提交？",
            OkText = "确认",
            OnOk = async (e) =>
            {
                var cache = FullCaches.FirstOrDefault(x => x.Deviceid == fullselet);
                if (cache != null)
                {
                    if (cache.SourId  != 0)
                    {
                        //立库领料
                        var unpks =await pickingServer.GetuNPK();
                        var unpk = unpks.FirstOrDefault(x=>x.Deviceid==cache.SourId);
                        var mac = machines.FirstOrDefault(x => x.Devid == cache.ToPostion);
                        if (mac != null)
                        {
                            if (unpk != null)
                            {
                                var b = await pickingServer.HostreturnCacheFromUnpack(cache, unpk, mac.MESName.Split(",").First());
                                await _message.Info(b);
                            }
                            else
                            {
                                await _message.Info("数据异常");

                            }
                        }
                        else
                        {
                            await _message.Info("没有匹配的设备");

                        }
                     

                    }
                    else
                    {
                            await _message.Info("暂无缓存架到拆包间的退料任务");

                    }
                }

            },
            CancelText = "取消"
        });


    }
}

